package com.jader.activities;

import java.util.ArrayList;
import java.util.List;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.adapter.MyPageAdapter;
import com.jader.fragment.AllOrderFragment;
import com.jader.fragment.BePaidFragment;
import com.jader.fragment.BeSendFragment;
import com.zlp.jader.R;

public class OrderActivity extends FragmentActivity {
	TextView BePaid, BeSend, AllOrder, tv_order_title;
	ViewPager viewPager;// 页卡内容
	ImageView imageView, img_order_back;
	List<Fragment> fragments;// Tab页面列表
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	private MyPageAdapter myPageAdapter;
	/** 页卡总数 **/
	private static final int pageSize = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collect_and_order);
		initView();
	}

	/** 初始化视图 */
	private void initView() {
		tv_order_title = (TextView) findViewById(R.id.top_actiobar_text);
		tv_order_title.setText("我的订单");
		img_order_back = (ImageView) findViewById(R.id.top_actionbar_back);
		// 初始化Tab标签
		AllOrder = (TextView) findViewById(R.id.Tab_one);
		BePaid = (TextView) findViewById(R.id.Tab_two);
		BeSend = (TextView) findViewById(R.id.Tab_three);

		BeSend.setText(R.string.BeSend);
		BePaid.setText(R.string.BePaid);
		AllOrder.setText(R.string.AllOrder);
		AllOrder.setTextColor(getResources().getColor(R.color.jader_theme));
		AllOrder.setOnClickListener(new MyOnClickListener(0));
		BePaid.setOnClickListener(new MyOnClickListener(1));
		BeSend.setOnClickListener(new MyOnClickListener(2));
		img_order_back.setOnClickListener(new MyOnClickListener(3));

		InitImageView();
		InitViewPager();
	}

	/**
	 * 初始化Viewpager页
	 */
	private void InitViewPager() {
		viewPager = (ViewPager) findViewById(R.id.Collect_And_order_viewpage);
		fragments = new ArrayList<Fragment>();
		fragments.add(new AllOrderFragment());
		fragments.add(new BePaidFragment());
		fragments.add(new BeSendFragment());
		myPageAdapter = new MyPageAdapter(getSupportFragmentManager(),
				fragments);
		viewPager.setAdapter(myPageAdapter);
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * 初始化动画，这个就是页卡滑动时，下面的横线也滑动的效果，在这里需要计算一些数据
	 */

	private void InitImageView() {
		imageView = (ImageView) findViewById(R.id.Tab_Select);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.tab_selected_bg).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / pageSize - bmpW) / 2;// 计算偏移量--(屏幕宽度/页卡总数-图片实际宽度)/2
													// = 偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		imageView.setImageMatrix(matrix);// 设置动画初始位置
	}

	/**
	 * 头标点击监听
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			switch (index) {
			case 0:
				break;
			case 1:

				break;
			case 2:
				break;
			case 3:
				finish();
			}
			viewPager.setCurrentItem(index);
		}

	}

	/**
	 * 为选项卡绑定监听器
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		public void onPageScrollStateChanged(int index) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int index) {
			Animation animation = new TranslateAnimation(one * currIndex, one
					* index, 0, 0);// 显然这个比较简洁，只有一行代码。
			currIndex = index;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			imageView.startAnimation(animation);
			switch (index) {
			case 0:
				reset();
				AllOrder.setTextColor(getResources().getColor(
						R.color.jader_theme));
				break;
			case 1:
				reset();
				BePaid.setTextColor(getResources()
						.getColor(R.color.jader_theme));
				break;
			case 2:
				reset();
				BeSend.setTextColor(getResources()
						.getColor(R.color.jader_theme));
				break;
			default:
				break;
			}
		}
	}

	private void reset() {
		BePaid.setTextColor(Color.BLACK);
		BeSend.setTextColor(Color.BLACK);
		AllOrder.setTextColor(Color.BLACK);
	}
}
